# -*- coding:utf-8 -*-
import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
from pymongo import MongoClient

# 易哈佛
from utils.HttpUtils import HttpUtils


class Test_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Connection": "keep-alive",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
        }


    def get_contents(self):
        postData = {
            "pageNum": "1",
            "numPerPage": "20",
            "title":"",
            "buyer_name":"",
            "agent_name":"",
            "proj_code":"",
            "bid_type": "108",
            "type":"",
            "dist_code": "340001",
            "pubDateStart":"",
            "pubDateEnd":"",
            "pProviceCode": "340000",
            "areacode_city": "340001",
            "areacode_dist":"",
            "channelCode": "sjcg_zbgg",
            "three":""

        }
        url = "https://www.ehafo.com/paper-127994"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        selector = etree.HTML(html.text)
        xpath = selector.xpath('//div[@class="main_choice"]')
        for i in xpath:
            dict_data = dict()
            num = 0
            dict_data['title'] = "".join(i.xpath('./p/a//text()'))
            for c in i.xpath('./text()'):
                tmp = c.replace("\r\n", "").strip()
                if (tmp != ""):
                    num = num + 1
                    dict_data['content_' + str(num)] = tmp
            da = i.xpath('./p[2]//text()')
            self.insertItem("aa", dict_data)

    def insertItem(self, tableName, data):
        my_set = db[tableName]
        my_set.insert_one(data)

if __name__ == '__main__':
    conn = MongoClient('127.0.0.1', 27017)
    db = conn["Test"]
    test_get = Test_Get()
    test_get.get_contents()